#from numpy import matrix
#from numpy import linalg
import os
import fileinput
import sys

files=['irrabbit']
graphexe='graph.exe'

for fname in files:

#    mname=sys.argv[1]
#    oname=sys.argv[2]
    mname = fname + '.m'
    oname = fname + '_connectivity.txt'
    glapname = fname + '_glap.txt'

    mf = fileinput.input(mname)

    cnt = 0
    vs = dict()

#read all vertices
    for line in mf:
        ar=line.split()
        if len(line)>1:
            if ar[0]=='Vertex':
                key=int(ar[1])
                vs[key]=cnt
                cnt += 1

    mf.close()
    mf=fileinput.input(mf.filename())

    of = open(oname,'w')
    lines=str(cnt)+'\n'
#now read all faces and create connectivity matrix
    for line in mf:
        ar=line.split()
        if len(line)>1 :
            if ar[0]=='Face':
                key1=int(ar[2])
                key2=int(ar[3])
                key3=int(ar[4])
                lines += (str(vs[key1])+' '+str(vs[key2])+'\n')
                lines += (str(vs[key2])+' '+str(vs[key3])+'\n')
                lines += (str(vs[key3])+' '+str(vs[key1])+'\n')

    of.write(lines)
